package com.daykw.douyin.bak;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.nutz.dao.Chain;
import org.nutz.dao.Cnd;
import org.nutz.dao.entity.Record;
import org.nutz.dao.impl.NutDao;

import com.daykw.douyin.config.NutzDaoConfig;
import com.daykw.douyin.http.HttpBase;
import com.daykw.douyin.model.VideoAuth;
import com.daykw.douyin.model.VideoUserSearch;
import com.daykw.douyin.utils.SpringContextHolder;

public class VideoSearchUserThread extends Thread {

	private static Logger logger = Logger.getLogger(VideoSearchUserThread.class);
	private NutDao dao = SpringContextHolder.getBean(NutzDaoConfig.class);
	private SimpleDateFormat fmt = new SimpleDateFormat("yyyyMMdd");

	@Override
	public void run() {

		List<VideoUserSearch> list = dao.query(VideoUserSearch.class, Cnd.where("status", "=", 0).limit(1, 10));

		HttpBase http = new HttpBase();

		for (VideoUserSearch v : list) {
			try {
				List<Record> cookies = dao.query("t_cookie", Cnd.wrap("WHERE 1=1 ORDER BY RAND() limit 1"));
				Record cookie = cookies.get(0);
				String c = cookie.getString("cookie");
				Map<String, String> headers = new HashMap<String, String>();
				headers.put("Cookie", c);
				String surl = "https://dy.feigua.cn/Blogger/Search?keyword=" + URLEncoder.encode(v.getName(), "utf-8");

				String html = http.get(surl, headers);
				Document doc = Jsoup.parse(html);
				if (doc.text().indexOf("抱歉，暂无搜索结果") >= 0) {
					dao.update(VideoUserSearch.class, Chain.make("status", 5), Cnd.where("id", "=", v.getId()));
					logger.error(v.getName() + "抱歉，暂无搜索结果");
					Thread.sleep(2000);
					continue;
				}

				Elements items = doc.select("div.v-search-item");
				if (items != null && items.size() > 0) {
					Element item = items.get(0);

					String name = item.select("span.nickname").text();

					String href = item.select("ul.v-zs-btns a").first().attr("href");
					String dataid = href.substring(href.indexOf("=") + 1, href.indexOf("&"));
					href = href.replace("#", "");
					String auth_url = "https://dy.feigua.cn" + href;

					VideoAuth auth = new VideoAuth();
					auth.setDataid(dataid);
					auth.setAuth_url(auth_url);
					auth.setKw_nick_name(name);
					auth.setVideo_status(0);
					auth.setSearch("user");
					auth.setStatus(0);
					auth.setCreate_time(new Date());
					auth.setCdate(Integer.parseInt(fmt.format(new Date())));
					try {
						dao.fastInsert(auth);
						dao.update(VideoUserSearch.class, Chain.make("status", 2).add("dataid", dataid),
								Cnd.where("id", "=", v.getId()));
					} catch (Exception e) {
						// logger.error("", e);
						logger.error("dataid重复:" + dataid);
						dao.update(VideoUserSearch.class, Chain.make("status", 3).add("dataid", dataid),
								Cnd.where("id", "=", v.getId()));
						dao.update(VideoAuth.class, Chain.make("status", 0).add("auth_url", auth_url),
								Cnd.where("dataid", "=", dataid));
					}
					Thread.sleep(2000);
				}

			} catch (

			Exception e) {
				dao.update(VideoUserSearch.class, Chain.make("status", 5), Cnd.where("id", "=", v.getId()));
				logger.error("", e);
			}
		}

	}

}
